/*测试数据*/
var testdata=[
	{
		id: 1,
		orederID: 'dfgdf3333',
		productID: 3434554,
		productBrand: 'lisd',
		productName: '上海新区',
		productVersion: '上海市浦东新区',
		singlePrice:230,
		num:2,
		user:{
			realName:"alal",
			phone:18817802281
		},
		state:1,
		deleted:0,
		isPayment:0,
		shoppingAddress:'上海市浦东新区aa',
		createTime: 1502937009
	},
	{
		id: 2,
		orederID: 'dfgdf3333',
		productID: 3434554,
		productBrand: 'lisd',
		productName: '上海新区',
		productVersion: '上海市浦东新区',
		singlePrice:230,
		num:2,
		user:{
			realName:"alal",
			phone:18817802281
		},
		state:2,
		deleted:0,
		isPayment:1,
		shoppingAddress:'上海市浦东新区aa',
		createTime: 1502937009
	},
	{
		id: 3,
		orederID: 'dfgdf3333',
		productID: 3434554,
		productBrand: 'lisd',
		productName: '上海新区',
		productVersion: '上海市浦东新区',
		singlePrice:230,
		num:2,
		user:{
			realName:"alal",
			phone:18817802281
		},
		state:3,
		deleted:0,
		isPayment:1,
		shoppingAddress:'上海市浦东新区aa',
		createTime: 1502937009
	},
	{
		id: 4,
		orederID: 'dfgdf3333',
		productID: 3434554,
		productBrand: 'lisd',
		productName: '上海新区',
		productVersion: '上海市浦东新区',
		singlePrice:230,
		num:2,
		user:{
			realName:"alal",
			phone:18817802281
		},
		state:1,
		deleted:0,
		isPayment:1,
		shoppingAddress:'上海市浦东新区aa',
		createTime: 1502937009
	}
];
var testoneitem={
		id: 1,
		orederID: 'dfgdf3333',
		productID: 3434554,
		productBrand: 'lisd',
		productName: '上海新区',
		productVersion: '上海市浦东新区',
		singlePrice:230,
		num:2,
		user:{
			realName:"alal",
			phone:18817802281
		},
		state:1,
		deleted:0,
		isPayment:1,
		shoppingAddress:'上海市浦东新区aa',
		createTime: 1502937009
};



$(function(){
	getTableData(-1,-1);
    $("#orderStateST").unbind().change(function(){
        var orderStateST = $("#orderStateST").val();
        var taxStatusST = $("#taxStatusST").val();
        getTableData(orderStateST,taxStatusST);
    });
    $("#taxStatusST").unbind().change(function(){
        var orderStateST = $("#orderStateST").val();
        var taxStatusST = $("#taxStatusST").val();
        getTableData(orderStateST,taxStatusST);
    });
});
/*获取表格数据*/
function getTableData(orderState,taxStatus){
	/*var backData=testdata;//[];
	tableInit(backData);*/
    var selectObject={};
    if(orderState>-1){
        selectObject.orderState=orderState;
    }
    if(taxStatus>-1){
        selectObject.taxStatus=taxStatus;
    }
    var tgid = localStorage.getItem("userID");
    if(tgid!=null && tgid!=""){
        selectObject.userId=tgid;
        $.post(
            gobalServer+"order/select",
            selectObject,
            function(backData){
                if(backData.statusCode==200){
                    var backData=backData.commonList;//[];
                    tableInit(backData);
                }else{
                    consoleMes($("#manageContent"),2000,"warning",backData.msg);//success、info、warning、danger

                }
            }
        );
    }else{
        consoleMes($("#manageContent"),3000,"warning","无法获取个人信息，可能账号未登录，请重新登录！");//success、info、warning、danger
        setTimeout(function () {
            window.location.href=gobalWeb+"login.html";
        }, 2500);
    }

};
/*表格初始化*/
function tableInit(backData){
	var objectArray=[];
    if(backData.length>0){
        objectArray=dataToJson(backData);
    }
    $('#stockTable').bootstrapTable('destroy').bootstrapTable({
        toolbar: '#toolbar',                //工具按钮用哪个容器
        pageNumber:1,                       //初始化加载第一页，默认第一页
        pageSize: 20,                       //每页的记录行数（*）
        minimumCountColumns: 2,             //最少允许的列数
        clickToSelect: true,                //是否启用点击选中行
        height: 500,                        //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
        data: objectArray,
        pageList: [5, 10, 20, 40, 100, 200]/*,
        rowStyle: function rowStyle(row,index) {
            if(row.mark!=null && row.mark>0){
                return {
                    css:{
                        "background-color": "#ffc930",
                        "color": "#fff"
                    }
                };
            }else{
                return {
                    css:{

                    }
                };
            }
        }*/
    });
};
/*表格数据封装*/
function dataToJson(dataResult) {
    var objectArray=new Array();
    $.each(dataResult,function(index,item){
        var createTime="--";
        if(item.createTime!=null){
            createTime=formatDateTimeYMDHMS(item.createTime);
        }
        var totalPrice="";
        if(item.totalPrice!=null){
            totalPrice="<b class='text-red'>￥"+item.totalPrice+"</a>";
        }

        var payTime="--";
        if(item.payTime!=null){
            payTime=formatDateTimeYMDHMS(item.payTime);
        }

        var orderMes="";
        orderMes="<a class='btn-a-common' href='javascript:;' tgid='"+item.orderId+"' onclick='getOrderMesDetail(this)' >查看</a>";

        var taxStatus="";
        if(item.taxStatus!=null && item.taxStatus==1){
            taxStatus="已开";
        }else{
            taxStatus="未开";
        }

        var orderState="";
        if(item.orderState!=null && item.orderState==2001){
            orderState="待支付";
        }else if(item.orderState!=null && item.orderState==2002){
            orderState="待确认";
        }else if(item.orderState!=null && item.orderState==2003){
            orderState="待发货";
        }else if(item.orderState!=null && item.orderState==2004){
            orderState="关闭";
        }
        
        var arr={
            "id":item.id,
            "orederID":item.orderId,
            /*"productName":item.productName,*/
            "totalPrice":totalPrice,
            "shoppingAddress":item.shoppingAddress,
            "orderMes":orderMes,
			/*"printMes":printMes,*/
			/*"isPayment":isPayment,*/
            "orderState":orderState,
            "taxStatus":taxStatus,
            "createTime":createTime,
            "payTime":payTime
        };
        objectArray.push(arr);
    });
    return objectArray;
};
/*查看订单详情*/
function getOrderMesDetail(obj){
    var tgid= $(obj).attr("tgid");
    if(tgid!=null && tgid!=""){
        $.post(
            gobalServer+"order/selectOne",
            {"orderId":tgid},
            function(backData){
                if(backData.statusCode==200){
                    var datarows=backData.object;
                    if(datarows!=null){
                        if(datarows.orderId!=null && datarows.orderId!=""){
                            getOrderDetailList(datarows.orderId);
                        }
                        $("#userPhone").text(datarows.userPhone);
                        $("#realName").text(datarows.realName);
                        /*$("#productBrand").text(datarows.productBrand);
                        $("#productName").text(datarows.productName);
                        $("#singlePrice").text("￥"+datarows.singlePrice);
                        $("#productVersion").text(datarows.productVersion);
                        $("#number").text(datarows.number);*/
                        $("#totalPrice").text("￥"+datarows.totalPrice);
                        var payTime="--";
                        if(datarows.payTime!=null){
                            payTime=formatDateTimeYMDHMS(datarows.payTime);
                        }
                        $("#payTime").text(payTime);
                        $("#shoppingAddress").text(datarows.shoppingAddress);
                        $("#orderRemark").text(datarows.orderRemark);
						/* if(datarows.paymentStatus!=null && datarows.paymentStatus==1){
						 $("#isPayment").text("已支付");
						 }else{
						 $("#isPayment").text("未支付");
						 }*/
						/*if(datarows.deleted!=null && datarows.deleted==1){
						 $("#deleted").text("已删除");
						 }else{
						 $("#deleted").text("未删除");
						 }*/
                        if(datarows.orderState==2004){
                            $("#hasShip").text("已发货");
                            $("#fareType").text(datarows.fareType);
                            if(datarows.fareStatus!=null && datarows.fareStatus==1){
                                $("#fareStatus").text("已收货");
                            }else{
                                $("#fareStatus").text("未收货");
                            }
                            var fareTime="--";
                            if(datarows.fareTime!=null){
                                fareTime=formatDateTimeYMDHMS(datarows.fareTime);
                            }
                            fareTime=formatDateTimeYMDHMS(fareTime);
                            /*fareTime=fareTime.substr(0,fareTime.indexOf(" "));*/
                            $("#fareTime").text(fareTime);
                            $("#hasShipGroup").show();
                        }
                        if(datarows.taxStatus==1){
                            $("#hasShip").text("已开票");
                            $("#taxNumber").text(datarows.taxNumber);
                            $("#taxMoney").text(datarows.taxMoney);
                            $("#taxGroup").show();
                        }
                        $("#orderModal").modal("show");
                    }else{
                        consoleMes($("#manageContent"),2000,"warning","无法获取数据，请刷新重试！");
                    }
                }else{
                    consoleMes($("#manageContent"),2000,"warning",backData.msg);//success、info、warning、danger

                }
            }
        );
	}else{
		consoleMes($("#manageContent"),2000,"warning","无法查看，请刷新页面重试！");//success、info、warning、danger
	}
};
/*获取订单详情*/
function getOrderDetailList(tid) {
    if(tid!=null && tid!=""){
        $.post(
            gobalServer+"order/selectOrderDetailByOrderId",
            {"orderId":tid},
            function(backData){
                if(backData.statusCode==200){
                    var datarows=backData.commonList;
                    $("#orderDetailList").empty();
                    if(datarows.length>0){
                        $.each(datarows,function (i,item) {
                            $productId=$("<td>").text(item.productId);
                            $productName=$("<td>").text(item.productName);
                            $productBrand=$("<td>").text(item.productBrand);
                            $productVersion=$("<td>").text(item.productVersion);
                            $singlePrice=$("<td>").text("￥"+item.singlePrice.toFixed(2));
                            $number=$("<td>").text(item.number);
                            $tr=$("<tr>");
                            $tr.append($productId,$productName,$productBrand,$productVersion,$singlePrice,$number);
                            $("#orderDetailList").append($tr);
                        });
                    }else{
                        $td=$("<td>").addClass("text-center").attr({"colspan":6}).text("暂无订单产品信息！");
                        $tr=$("<tr>");
                        $tr.append($td);
                        $("#orderDetailList").append($tr);
                    }
                }else{
                    consoleMes($("#manageContent"),2000,"warning",backData.msg);//success、info、warning、danger

                }
            }
        );
    }else {
        consoleMes($("#manageContent"),2000,"warning","无法获取订单详情信息，请刷新页面重试！");//success、info、warning、danger
    }
}